package com.x.mvp.widget.dateselector.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;


import com.x.mvp.R;
import com.x.mvp.widget.dateselector.view.WheelView;

import java.util.ArrayList;
import java.util.List;


/**
 * 日期选择对话框
 * <p/>
 * Created by huangzj on 2015/10/25.
 */
public class SelectDialog extends BaseDialog {

    private View dialogView;
    private WheelView rellationWheel;

    private OnClickListener onClickListener;

    List<String> items;

    /**
     * 创建一个日期选择对话框
     *
     * @param mContext
     */
    public SelectDialog(Context mContext, List<String> items) {
        this.context = mContext;
        this.items = items;
        create();
    }

    public SelectDialog(Context mContext, String[] items) {
        this.context = mContext;
        ArrayList<String> list = new ArrayList<String>();
        for (String item : items) {
            list.add(item);
        }
        this.items = list;
        create();
    }

    public void setItems(List<String> items) {
        this.items = items;
        if (rellationWheel != null) {
            rellationWheel.setWheelItemList(items);
        }
    }

    /**
     * 创建一个日期选择对话框
     *
     * @param mContext
     */
    public SelectDialog(Context mContext, OnClickListener listener) {
        this.context = mContext;
        onClickListener = listener;
        create();
    }

    /**
     * 创建选择日期对话框
     */
    private void create() {

        if (dialog != null) {
            return;
        }
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        dialogView = layoutInflater.inflate(R.layout.dialog_wheel_select_relation, null);
        rellationWheel = (WheelView) dialogView.findViewById(R.id.select_date_relation_wheel);

        rellationWheel.setWheelItemList(items);
        rellationWheel.setOnSelectListener(new WheelView.SelectListener() {
            @Override
            public void onSelect(int index, String text) {

            }
        });

        Button cancelBt = (Button) dialogView.findViewById(R.id.select_date_cancel);
        cancelBt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    if (!onClickListener.onCancel()) {
                        dialog.dismiss();
                    }
                } else {
                    dialog.dismiss();
                }
            }
        });
        Button sureBt = (Button) dialogView.findViewById(R.id.select_date_sure);
        sureBt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (onClickListener != null) {
                    if (!onClickListener.onSure(rellationWheel.getCurrentItem(), items.get(rellationWheel.getCurrentItem()))) {
                        dialog.dismiss();
                    }
                } else {
                    dialog.dismiss();
                }
            }
        });
        dialog = new Dialog(context, R.style.quick_dialog);
        initDialog();
        dialog.setContentView(dialogView);
    }

    /**
     * 显示选择日期对话框
     */
    public void show() {
        if (dialog == null || dialog.isShowing()) {
            return;
        }

        dialog.show();
    }


    /**
     * 选择日期对话框回调
     *
     * @param listener
     */
    public void setOnClickListener(OnClickListener listener) {
        onClickListener = listener;
    }

    /**
     * 选择日期对话框回调接口，调用者实现
     *
     * @author huangzj
     */
    public interface OnClickListener {
        boolean onSure(int index, String data);

        boolean onCancel();
    }
}
